<!-- <template>
    <ContentField>
        <div class="row">
            <div class="col-3"> 
                <img class="img-fluid" src="https://ts1.cn.mm.bing.net/th?id=OIP-C.bj4bXnzhQFY2euPlr2TpFQHaLH&w=135&h=185&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2">
            </div>
            <div class="col-9">
                <div class="row goods-name">商品名称</div>
                <div class="row goods-description">商品描述</div>
                <div class="row goods-price">175</div>
                <button type="button" class="btn-mybtn">立即下单！</button>
            </div>
        </div>
    </ContentField>
</template>

<script>
import ContentField from '@/components/ContentField.vue'

export default {
    components: {
        ContentField
    }
}
</script>

<style scoped>
.goods-name {
    font-weight: bold;
    font-size: 2rem;
    color: black;
}
.goods-description {
    color: grey;
}
.goods-price {
    font-size: 1.5rem;
    color: #FF0036;
}
.btn-mybtn {
    border: 2px solid red;
    border-radius: 5px;
    background-color: #FF0036;
    color: white;
}
</style>

 -->
 <template>
    <div class="container">
        <div class="col-md-12">
            <div v-if="isProductLoading" class="text-center">
                <grid-loader :loading="isProductLoading" :color="loaderColor" :size="loaderSize" class="d-inline-block" />
            </div>
            <div v-else class="card">
                <div class="row">
                    <div class="col-12 col-md-4 offset-md-4">
                        <div class="intrinsic">
                        <img class="img-fluid intrinsic-item" :src="item.thumbnail_url" alt="">
                        </div>
                    </div>
                </div>

                <div class="caption-full">
                    <h4 class="pull-right">￥ {{ item.price }}</h4>
                    <h4> {{ item.title }}</h4>
                    <p> {{ item.description }} </p>
                </div>
                <div class="ratings">
                    <span>在售：{{ item.quantity }}</span>
                    <p class="pull-right">
                        <button @click="addItem" :disabled="item.quantity === 0" class="btn btn-success">加入购物车</button>
                    </p>
                    <div class="clearfix"></div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { computed } from '@vue/runtime-core'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import GridLoader from 'vue-spinner/src/GridLoader.vue'

export default {
    components: {
        GridLoader
    },
    setup() {
        const store = useStore()
        const route = useRoute()
        let isProductLoading = computed(() => {return store.state.goods.isProductLoading;})
        let goodsList = computed(() => {return store.getters.goodsList;})
        let item = computed(() => {
            let id = route.params.id;
            if (goodsList.value.length >= id) {
                let filterArr = goodsList.value.filter((item) => {
                    return item.id == id
                });
                if (filterArr.length > 0) {
                    return filterArr[0];
                }
            }
            return {};
        })

        const updateShoppingList = (order) => {
            store.dispatch('updateShoppingList', order)
        }
        const addItem = () => {
            const order = {
                item: Object.assign({}, item.value),
                quantity: 1,
                isAdd: true
            };
            updateShoppingList(order)
        }

        return {
            loaderColor: "#5cb85c",
            loaderSize: "50px",
            isProductLoading,
            goodsList,
            item,
            updateShoppingList,
            addItem
        }
    }
}
</script>

<style scoped>
.caption-full {
    padding-right: 10px;
    padding-left: 10px;
}

.ratings {
    padding-right: 10px;
    padding-left: 10px;
    color: #d17581;
}
</style>
    